home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 November / Chip Kasım 2003.iso / prog / openoff / f_0155 / FormWizard.xba < prev    next >
Encoding:
Extensible Markup Language  |  2003-02-20  |  12.1 KB  |  379 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="FormWizard" script:language="StarBasic">Option Explicit
  4.  
  5. Public DocumentName as String
  6. Public FormPath as String
  7. Public WizardPath as String
  8. Public WebWizardPath as String
  9. Public WorkPath as String
  10. Public TexturePath as String
  11. Public sQueryName as String
  12. Public oDBConnection as Object
  13. Public bWithBackGraphic as Boolean
  14. Public bNeedFieldRefresh as Boolean
  15. Public oDBForm as Object
  16. Public oColumns() as Object
  17. Public sDatabaseList() as String
  18. Public TableNames() as String
  19. Public QueryNames() as String
  20. Public FieldNames() as String
  21. Public ImgFieldNames() as String
  22. Public oDBContext as Object
  23. Public oUcb as Object
  24. Public oDocInfo as Object
  25. Public WidthList(15,3)
  26. Public ImgWidthList(3,3)
  27. Public sDBName as String
  28. Public Tablename as String
  29. Public Const SBSIZETEXT = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
  30. Public bDisposeDoc as Boolean
  31. Public bDebug as Boolean
  32.  
  33. ' The macro can be called in 4 possible scenarios:
  34. ' Scenario 1. No parameters at given
  35. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  36. ' Scenario 3: a data source and a connection are given
  37. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  38.  
  39. Sub Main()
  40. Dim oLocDBContext as Object
  41. Dim oLocConnection as Object
  42.  
  43. ' Scenario 1. No parameters at given
  44.     MainWithDefault()
  45.  
  46. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  47. '    MainWithDefault("Bibliography")
  48.  
  49. ' Scenario 3: a data source and a connection are given
  50. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  51. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  52. '    MainWithDefault("Bibliography", oLocConnection)
  53.  
  54. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  55. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  56. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  57. '    MainWithDefault("Bibliography", oLocConnection, com.sun.star.sdb.CommandType.TABLE, "biblio")
  58. End Sub
  59.  
  60.  
  61. Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String)
  62. Dim i as Integer
  63. Dim SelCount as Integer
  64. Dim RetValue as Integer
  65. Dim SelList(0) as Integer
  66.     SelList(0) = 0
  67.     BasicLibraries.LoadLibrary("Tools")
  68.     BasicLibraries.LoadLibrary("WebWizard")
  69.     bDebug = False
  70.     If Not bDebug Then
  71.         On Local Error GoTo WIZARDERROR
  72.     End If
  73.     OpenFormDocument()
  74.     CurArrangement = 0
  75.     bControlsareCreated = False
  76.     bEnableBinaryOptionGroup = False
  77.     bDisposeDoc = True
  78.     MaxIndex = -1
  79.     If Not InitResources("Formwizard","dbw") Then
  80.         Exit Sub
  81.     End If
  82.     oDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  83.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  84.     If GetFormWizardPaths() = False Then
  85.         Exit Sub
  86.     End If
  87.     oDocument.GetCurrentController().Frame.ComponentWindow.Enable = False
  88.     oProgressBar.Value = 10
  89.     LoadLanguage()
  90.     oProgressBar.Value = 20
  91.     InitializeWidthList()
  92.     oProgressBar.Value = 30
  93.     Styles() = getListBoxArrays(oUcb, "/stl")
  94.     CurIndex = GetCurIndex(DialogModel, Styles(), 2)
  95.     oProgressBar.Value = 40
  96.     ConfigurePageStyle()
  97.     oProgressBar.Value = 50
  98.     InitializeLabelValues()
  99.     bNeedFieldRefresh = True
  100.     SetDialogLanguage()
  101.     With DialogModel
  102.         .cmdBack.Enabled = False
  103.         .cmdGoOn.Enabled = False
  104.         .lblTables.Enabled = False
  105.         .lstSelFields.Tag = False
  106.         .Step = 1
  107.         .lstDatabases.StringItemList()= GetDatabaseNames(IsMissing(DataSourceName))
  108.         .lstDatabases.SelectedItems() = Array(0)
  109.     End With
  110.     oProgressBar.Value = 60
  111.     If Not IsMissing(DataSourceName) Then
  112.         sDBName = DataSourceName
  113.         DlgFormDB.GetControl("lstDatabases").SelectItem(DataSourceName, True)
  114.         If Not IsMissing(oConnection) Then
  115.             ' Scenario 3: a data source and a connection are given
  116.             Set oDBConnection = oConnection
  117.             oDataSource = oDBContext.GetByName(DataSourceName)
  118.             DialogModel.lstTables.Enabled = True
  119.             DialogModel.lblTables.Enabled = True
  120.             If GetDBMetaData() Then
  121.                 DialogModel.lstTables.StringItemList() = AddListToList(TableNames(), QueryNames())
  122.                 iCommandTypes = CreateCommandTypeList()
  123.                 If Not IsMissing(sContent) Then
  124.                     ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  125.                     iCommandTypes() = CreateCommandTypeList()
  126.                     SelCount = CountItemsInArray(DialogModel.lstTables.StringItemList(), sContent)
  127.                     If SelCount = 1 Then
  128.                         DlgFormDB.GetControl("lstTables").SelectItem(sContent, True)
  129.                     Else
  130.                         If CommandType = com.sun.star.sdb.CommandType.QUERY Then
  131.                             SelIndex = IndexInArray(sContent, QueryNames()
  132.                             DlgFormDB.GetControl("lstTables").SelectItemPos(SelIndex, True)
  133.                         ElseIf CommandType = com.sun.star.sdb.CommandType.TABLE Then
  134.                             SelIndex = IndexInArray(sContent, TableNames()
  135.                             DlgFormDB.GetControl("lstTables").SelectItemPos(Ubound(QueryNames()+1 + SelIndex, True)
  136.                         End If
  137.                     End If
  138.                     CurCommandType = CommandType
  139.                     FillUpFieldsListbox(False)
  140.                 End If
  141.             End If
  142.         Else
  143.             ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  144.             GetSelectedDBMetaData()
  145.         End If
  146.     Else
  147.         ' Scenario 1: No parameters are given
  148.         ToggleListboxControls(DialogModel, False)
  149.     End If
  150.     oProgressBar.Value = 80
  151.     bWithBackGraphic = LoadNewStyles(oDocument, DialogModel, CurIndex, Styles(CurIndex, 8), Styles(), TexturePath)
  152.     DlgFormDB.Title = WizardTitle(1)
  153.     DialogModel.lstStyles.StringItemList() = ArrayfromMultiArray(Styles, 1)
  154.     DialogModel.lstStyles.SelectedItems() = SelList()
  155.     ControlCaptionsToStandardLayout()
  156.     oDocument.GetCurrentController().Frame.ComponentWindow.Enable = True
  157.     oProgressBar.Value = 90
  158.     DialogModel.imgTheme.ImageURL = FormPath & "FormWizard_1.bmp"
  159.     DialogModel.imgTheme.BackGroundColor = RGB(0,60,126)
  160.     ToggleDatabasePage(True)
  161.     oProgressBar.Value = 100
  162.     DlgFormDB.GetControl("lstDatabases").SetFocus()
  163.     oProgressbar.End
  164.     RetValue = DlgFormDB.Execute()
  165.     DlgFormDB.Dispose()
  166.     If Not IsNull(oDBConnection) Then
  167.         oDBConnection.Dispose()
  168.     End If
  169.     If bDisposeDoc Then
  170.         DisposeDocument(oDocument)
  171.     ElseIf RetValue = 0 Then
  172.         RemoveNirwanaShapes()
  173.     End If
  174. WIZARDERROR:
  175.     If Err <> 0 Then
  176.         Msgbox(sMsgErrMsg, 16, GetProductName())
  177.         Resume LOCERROR
  178.         LOCERROR:
  179.     End If
  180. End Sub
  181.  
  182.  
  183. Sub FormGetFields()
  184. Dim i as Integer
  185.     DeleteFirstListBoxEntry("lstTables", sSelectDBTable)
  186.     ToggleDatabasePage(False)
  187.     FillUpFieldsListbox(True)
  188.     ToggleDatabasePage(True)
  189. End Sub
  190.  
  191.  
  192. Sub FillUpFieldsListbox(bGetCommandType as Boolean)
  193. Dim SelIndex as Integer
  194. Dim QueryIndex as Integer
  195.     If Not bDebug Then
  196.         On Local Error GoTo NOFIELDS
  197.     End If
  198.     SelIndex = DlgFormDB.GetControl("lstTables").getSelectedItemPos() '.SelectedItems())
  199.     If SelIndex > -1 Then
  200.         If bGetCommandType Then
  201.             CurCommandType = iCommandTypes(SelIndex)
  202.         End If
  203.         If CurCommandType = com.sun.star.sdb.CommandType.QUERY Then
  204.             QueryIndex = SelIndex  - Ubound(Tablenames()) - 1
  205.             Tablename = QueryNames(QueryIndex)
  206.             oColumns = oDBConnection.Queries.GetByName(TableName).Columns
  207.         Else
  208.             Tablename = Tablenames(SelIndex)
  209.             oColumns = oDBConnection.Tables.GetByName(Tablename).Columns
  210.         End If
  211.         If GetSpecificFieldNames() <> -1 Then
  212.             ToggleListboxControls(DialogModel, True)
  213.             Exit Sub
  214.         End If
  215.     End If
  216.     EmptyFieldsListboxes()
  217. NOFIELDS:
  218.     If Err <> 0 Then
  219.         MsgBox sMsgErrCouldNotOpenObject, 16, sMsgWizardName
  220.     End If
  221. End Sub
  222.  
  223.  
  224. Sub PreviousStep()
  225.     If Not bDebug Then
  226.         On Local Error GoTo WIZARDERROR
  227.     End If
  228.     With DialogModel
  229.         .Step = 1
  230.         .cmdBack.Enabled = False
  231.         .cmdGoOn.Enabled = True
  232.         .lstSelFields.Tag = Not bControlsareCreated
  233.         .cmdGoOn.Label = sGoOn
  234.         .imgTheme.ImageUrl = FormPath & "FormWizard_1.bmp"
  235.     End With
  236.     FormSetMoveRights()
  237. WIZARDERROR:
  238.     If Err <> 0 Then
  239.         Msgbox(sMsgErrMsg, 16, GetProductName())
  240.         Resume LOCERROR
  241.         LOCERROR:
  242.     End If
  243. End Sub
  244.  
  245.  
  246. Sub NextStep()
  247.     If Not bDebug Then
  248.         On Local Error GoTo WIZARDERROR
  249.     End If
  250.     Select Case DialogModel.Step
  251.         Case 1
  252.             bControlsAreCreated = Not (cBool(DialogModel.lstSelFields.Tag))
  253.             If Not bControlsAreCreated Then
  254.                 GetTableMetaData()
  255.                 CreateDBForm()
  256.                 RemoveShapes()
  257.                 InitializeLayoutSettings()
  258.                 oDBForm.Load
  259.             End If
  260.             DialogModel.cmdGoOn.Label = sReady
  261.             DialogModel.cmdBack.Enabled = True
  262.             DialogModel.Step = 2
  263.             bDisposeDoc = False
  264.         Case 2
  265.             StoreForm()
  266.     End Select
  267.     DialogModel.imgTheme.ImageUrl = FormPath & "FormWizard_" & DialogModel.Step & ".bmp"
  268.     DlgFormDB.Title = WizardTitle(DialogModel.Step)
  269. WIZARDERROR:
  270.     If Err <> 0 Then
  271.         Msgbox(sMsgErrMsg, 16, GetProductName())
  272.         Resume LOCERROR
  273.         LOCERROR:
  274.     End If
  275. End Sub
  276.  
  277.  
  278. Sub InitializeLayoutSettings()
  279.     SwitchArrangementButtons(cTabled)
  280.     SwitchAlignMode(SBALIGNLEFT)
  281.     SwitchBorderMode(SB3DBORDER)
  282.     ToggleBorderGroup(bControlsAreCreated)
  283.     ToggleAlignGroup(bControlsAreCreated)
  284.     ArrangeControls()
  285.     If OldAlignMode <> 0 Then
  286.         DlgFormDB.GetControl("optAlign2").Model.State = 0
  287.     End If
  288. End Sub
  289.  
  290.  
  291. Sub ToggleDatabasePage(bDoEnable as Boolean)
  292.     With DialogModel
  293.         .cmdBack.Enabled = False
  294.         .cmdHelp.Enabled = bDoEnable
  295.         .cmdGoOn.Enabled = Ubound(DialogModel.lstSelFields.StringItemList()) <> -1
  296.         .hlnBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  297.         .optIgnoreBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  298.         .optBinariesasGraphics.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  299.     End With
  300. End Sub
  301.  
  302.  
  303. ' This Sub is called from the Procedure "StoreDocument" in the "Tools" Library
  304. Sub CommitLastDocumentChanges(sTargetPath as String)
  305. Dim i as Integer
  306. Dim sBookmarkName as String
  307. Dim oDBBookmarks as Object
  308. Dim bLinkExists as Boolean
  309. Dim sBaseBookmarkName as String
  310.     sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath))
  311.     sBaseBookmarkName = sBookmarkName
  312.     oDBBookmarks = oDataSource.GetBookmarks()
  313.     i = 1
  314.     Do
  315.         bLinkExists = oDBBookmarks.HasbyName(sBookmarkName)
  316.         If bLinkExists Then
  317.             i = i + 1
  318.             sBookmarkName = sBaseBookmarkName & "_" & i
  319.         Else
  320.             oDBBookmarks.insertByName(sBookmarkName, sTargetPath)
  321.         End If
  322.     Loop Until Not bLinkExists
  323.     bDisposeDoc = False
  324.     GroupShapesTogether()
  325.     ToggleDesignMode(oDocument)
  326.     oDBForm.Reload()
  327. End Sub
  328.  
  329.  
  330. Sub StoreForm()
  331. Dim sTargetPath as String
  332. Dim TypeNames(0,2) as String
  333. Dim oMasterKey as Object
  334. Dim oTypes() as Object
  335.     oMasterKey = GetRegistryKeyContent("org.openoffice.Office.TypeDetection/")
  336.     oTypes() = oMasterKey.Filters
  337.     TypeNames(0,0) = GetFilterName("StarOffice XML (Writer)")
  338.     TypeNames(0,1) = "*.sxw"
  339.     TypeNames(0,2) = ""
  340.     sTargetPath = StoreDocument(oDocument, TypeNames(), "Form_" & sDBName & "_" & TableName & ".sxw", WorkPath, 1)
  341.     If sTargetPath <> "" Then
  342.         DlgFormDB.EndExecute()
  343.     End If
  344. End Sub
  345.  
  346.  
  347. Sub EmptyFieldsListboxes()
  348. Dim NullList() as String
  349.     ToggleListboxControls(DialogModel, False)
  350.     DialogModel.lstFields.StringItemList() = NullList()
  351.     DialogModel.lstSelFields.StringItemList() = NullList()
  352.     bEnableBinaryOptionGroup = False
  353. End Sub
  354.  
  355.  
  356. Sub DeleteFirstDBListBoxEntry()
  357.     DeleteFirstListBoxEntry("lstDatabases", sSelectDatasource)    
  358. End Sub
  359.  
  360.  
  361. Sub DeleteFirstTableListBoxEntry()
  362.     DeleteFirstListBoxEntry("lstTables", sSelectDBTable)
  363. End Sub
  364.  
  365. Sub DeleteFirstListboxEntry(ListBoxName as String, DelEntryName as String)
  366. Dim oListbox as Object
  367. Dim sFirstItem as String
  368. dim iSelPos as Integer
  369.     oListBox = DlgFormDB.getControl(ListBoxName)
  370.     sFirstItem = oListBox.getItem(0)
  371.     If sFirstItem = DelEntryName Then
  372.         iSelPos = oListBox.getSelectedItemPos()
  373.         oListBox.removeItems(0, 1)
  374.         If iSelPos > 0 Then
  375.             oListBox.selectItemPos(iSelPos-1, True)
  376.         End If
  377.     End If
  378. End Sub
  379. </script:module>